<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h2>{{ msg }}</h2>
      <h2>总价格： {{totalPrice}} 元</h2>
    </div>
    <script src="../node_modules/vue/dist/vue.js"></script>
    <script>
      new Vue({
        el: "#app",
        data: {
          msg: "hello,world",
          books: [
            {
              id: 110,
              name: "Unix编程的艺术",
              price: 99,
            },
            {
              id: 111,
              name: "代码大全",
              price: 99,
            },
            {
              id: 112,
              name: "java编程",
              price: 99,
            },
          ],
        },
        computed: {
          totalPrice() {
            // let total=0;
            // this.books.forEach(element => {
            //    total+= element.price
            // });
            // return total;
            return this.books.reduce((x,y)=>x+=y.price,0);

          }
        },
      });
    </script>
  </body>
</html>
